using BusinessObject;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace Autenticazione
{
	public class Coaches : Page
	{
		protected HtmlForm form1;
		protected Label msgLoggedUser;
		protected DataGrid dtgCoach;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (this.Session["EMail"] == null)
			{
				base.Response.Redirect("Autenticazione.aspx");
			}
			this.msgLoggedUser.Text = string.Format("Utente corrente: {0}", this.Session["EMail"].ToString());
			if (!base.IsPostBack)
			{
				this.LoadGrid();
			}
		}
		private void LoadGrid()
		{
			BusinessObject.Coach c = new BusinessObject.Coach();
			if (this.Session["EMail"] == null)
			{
				return;
			}
			DataTable dt = c.GetTbCoaches(this.Session["Email"].ToString());
			DataTable dtView = dt.Clone();
			for (int i = this.dtgCoach.CurrentPageIndex * this.dtgCoach.PageSize; i <= (this.dtgCoach.CurrentPageIndex + 1) * this.dtgCoach.PageSize - 1; i++)
			{
				if (i <= dt.Rows.Count - 1)
				{
					dtView.ImportRow(dt.Rows[i]);
				}
			}
			this.dtgCoach.VirtualItemCount = dt.Rows.Count;
			this.dtgCoach.DataSource = dtView;
			this.dtgCoach.DataBind();
		}
		protected void dtgCoach_Edit(object sender, DataGridCommandEventArgs e)
		{
			this.dtgCoach.EditItemIndex = e.Item.ItemIndex;
			this.LoadGrid();
		}
		protected void dtgCoach_Delete(object sender, DataGridCommandEventArgs e)
		{
			new BusinessObject.Coach
			{
				Line = long.Parse(this.dtgCoach.DataKeys[e.Item.ItemIndex].ToString())
			}.DeleteCoach();
			this.dtgCoach.EditItemIndex = -1;
			this.LoadGrid();
		}
		protected void dtgCoach_Cancel(object sender, DataGridCommandEventArgs e)
		{
			this.dtgCoach.EditItemIndex = -1;
			this.LoadGrid();
		}
		protected void dtgCoach_Update(object sender, DataGridCommandEventArgs e)
		{
			BusinessObject.Coach c = new BusinessObject.Coach();
			c.Line = long.Parse(this.dtgCoach.DataKeys[e.Item.ItemIndex].ToString());
			TextBox allenatore = e.Item.FindControl("txtAllenatore") as TextBox;
			TextBox telefono = e.Item.FindControl("txtPhone") as TextBox;
			if (allenatore != null)
			{
				c.FullName = allenatore.Text.Replace("'", "''");
			}
			if (telefono != null)
			{
				c.Phone = telefono.Text;
			}
			c.Update();
			this.dtgCoach.EditItemIndex = -1;
			this.LoadGrid();
		}
		protected void dtgCoach_SelectedIndexChanged(object sender, DataGridPageChangedEventArgs e)
		{
			this.dtgCoach.CurrentPageIndex = e.NewPageIndex;
			this.dtgCoach.EditItemIndex = -1;
			this.LoadGrid();
		}
	}
}
